import urllib3
import re

# !分析页面     检查网页源码里是否有自己需要的资源

# 2根据页面的url获取整个页面的资源
page_url = "http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1584437043954_R&pv=&ic=&nc=1&z=&hd=&latest=&copyright=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&hs=2&sid=&word=%E7%86%8A%E7%8C%AB"
http = urllib3.PoolManager()  # 连接池对象

res = http.request("get", page_url)
html = res.data.decode("utf-8")

# print(html)

# 2匹配图片url
img_urls = re.findall(r'"thumbURL":"(.*?)"', html)
print(img_urls)

# 3定位图片资源
# for img_url in img_urls:
#     res=http.request("GET",img_url)
#     print(res.data)
# 构造请求头
headers = {
    "Referer": "http://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gb18030&word=%CD%BC%C6%AC&fr=ala&ala=1&alatpl=adress&pos=0&hs=2&xthttps=000000",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0"}
for index, img_url in enumerate(img_urls):
    print(index)
    # if "\\" in img_url:
    #     img_url = img_url.replace("\\", "")
    res = http.request("GET", img_url, headers=headers)
    print(res.data)
    suffix = img_url.split(".")[-1]  # 拿文件后缀格式

    imgname = "pandas_cats/img" + str(index) + "." + suffix
    with open(imgname, "wb") as f:
        f.write(res.data)
print("ok")
